package cn.airfei.aircore.testcore.services.domainService.impl;

import cn.airfei.aircore.core.properties.AirCoreProperty;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import cn.airfei.aircore.core.utils.QueryParamUtil;
import cn.airfei.aircore.testcore.services.entity.Stock;
import cn.airfei.aircore.testcore.services.mapper.StockMapper;
import cn.airfei.aircore.testcore.services.domainService.StockDomainService;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import cn.airfei.aircore.testcore.storage.dto.StockDto;
import cn.airfei.aircore.testcore.storage.vo.StockVo;
import org.springframework.stereotype.Service;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import org.apache.commons.lang3.math.NumberUtils;
import cn.airfei.aircore.core.constants.AirStatusConstants;
import javax.annotation.Resource;
import java.util.Arrays;
import java.util.List;
import java.util.Map;
import java.util.stream.Collectors;
import cn.airfei.aircore.core.storage.PageParam;

/**
 * <p>
 * 库存
 * </p>
 *
 * @author air
 * @since 2022-04-24
 */
@Service
public class StockDomainServiceImpl extends ServiceImpl<StockMapper, Stock> implements StockDomainService {
    @Resource
    private AirCoreProperty airCoreProperty;

    @Resource
    private StockMapper stockMapper;


    /**
     *   
     * @param stockDto
     * @param pageParam
     * @return
     */
    @Override
    public Page<StockVo> index(StockDto stockDto,PageParam pageParam) {
        Page<StockDto> page=new Page<>(pageParam.getPageCurrent(),pageParam.getPageSize());
        return stockMapper.index(page,stockDto,pageParam);
    }

    /**
     * 分页查询单表数据
     *
     * @param map
     * @return
     */
    @Override
    public Page<Stock> singleTablePage(Map<String, Object> map) {
        QueryWrapper<Stock> queryWrapper= QueryParamUtil.getEntityWrapper(map,Stock.class);
        queryWrapper.lambda().orderByDesc(Stock::getId);
        Page<Stock> page=new Page<>(Long.parseLong(String.valueOf(map.get("pageCurrent"))),Long.parseLong(String.valueOf(map.get("pageSize"))));
        return stockMapper.selectPage(page,queryWrapper);
    }

    /**
     * 显示单条数据
     *
     * @param id
     * @return
     */
    @Override
    public Stock show(String id){
        QueryWrapper<Stock> queryWrapper=new QueryWrapper<>();
        queryWrapper.eq("id",id);
        return stockMapper.selectOne(queryWrapper);
    }

    /**
     * 删除（销毁表数据）,逻辑删除,设置status为-1
     *
     * @param ids
     * @return
     */
    @Override
    public Integer destroy(String ids){
        List<Long> idLongList = Arrays.asList(ids.split(",")).stream()
            .map(s -> NumberUtils.createLong(s.trim())).collect(Collectors.toList());

        QueryWrapper<Stock> queryWrapper=new QueryWrapper();
        queryWrapper.in("id",idLongList);
        Stock stock=new Stock();
        stock.setStatus(AirStatusConstants.delete);
        return stockMapper.update(stock,queryWrapper);
    }

    /**
     * 更新表数据
     *
     * @param stock
     * @return
     */
    @Override
    public Integer update(Stock stock){
        return stockMapper.updateById(stock);
    }

    /**
     * 保存表数据
     *
     * @param stock
     * @return
     */
    @Override
    public Integer store(Stock stock){
        return stockMapper.insert(stock);
    }

    /**
     * 设置状态，1:正常,2:禁用/待审核，-1:已删除
     *
     * @param id
     * @param status
     * @return
     */
    @Override
    public Integer setStatus(Long id, Integer status) {
        Stock stock=new Stock();
        stock.setId(id);
        stock.setStatus(status);
        return stockMapper.updateById(stock);
    }

    /**
     * get all resource in storage
     * @return
     */
    @Override
    public List<Stock> getAllStock() {
        QueryWrapper<Stock> queryWrapper=new QueryWrapper();
        queryWrapper.lambda()
            .orderByDesc(Stock::getId)
            .last("limit " + airCoreProperty.getMaxSelect());
        return stockMapper.selectList(queryWrapper);
    }

    /**
     * get list in single table
     *
     * @param map
     * @return
     */
    @Override
    public List<Stock> getSingleTableList(Map map) {
        QueryWrapper<Stock> queryWrapper = QueryParamUtil.getEntityWrapper(map, Stock.class);
        queryWrapper.lambda().orderByDesc(Stock::getId).last("limit " + airCoreProperty.getMaxSelect());
        return stockMapper.selectList(queryWrapper);
    }

}
